package com.doujiang.Controller;

import com.doujiang.domain.Comment;
import com.doujiang.domain.dto.HotCommentDTO;
import com.doujiang.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comments")
@CrossOrigin
public class CommentController {

    @Autowired
    private ICommentService commentService;

    /**
     * 根据用户ID获取用户的所有评论
     */
    @GetMapping("/user/{userId}")
    public ResponseEntity<?> getUserComments(@PathVariable("userId") Long userId) {
        List<Comment> comments = commentService.getCommentsByUserId(userId);
        return ResponseEntity.ok(comments);
    }

    /**
     * 获取热门评论
     */
    @GetMapping("/hot")
    public ResponseEntity<?> getHotComments(@RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        List<HotCommentDTO> hotComments = commentService.getHotComments(limit);
        return ResponseEntity.ok(hotComments);
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteComment(@PathVariable("id") Long id) {
        boolean success = commentService.removeById(id);
        if (success) {
            return ResponseEntity.ok("评论删除成功");
        } else {
            return ResponseEntity.status(500).body("评论删除失败");
        }
    }

    /**
     * 添加评论
     */
    @PostMapping
    public ResponseEntity<?> addComment(@RequestBody Comment comment) {
        boolean success = commentService.save(comment);
        if (success) {
            return ResponseEntity.ok(comment);
        } else {
            return ResponseEntity.status(500).body("评论添加失败");
        }
    }
} 